package com.tul.vapp.servicecenter.controller;

import com.tul.cusa.common.exception.BizException;
import com.tul.vapp.servicecenter.bean.ResponseBean;
import com.tul.vapp.servicecenter.common.ErrorCodeConstant;
import com.tul.vapp.servicecenter.common.SecurityConstant;
import com.tul.vapp.servicecenter.entity.User;
import com.tul.vapp.servicecenter.service.IUserService;
import com.tul.vapp.servicecenter.util.Des;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;


/**
 * base controller
 *
 * @author wujunlei
 * @version 1.0
 * @since 2018/1/2
 */
@RestController
public class BaseController extends ExceptionController {

    /**
     * session
     */
    @Autowired
    HttpSession session;
    /**
     * base service
     */
    @Autowired
    private IUserService service;

    /**
     * login
     *
     * @param params 入参
     * @return str
     */
    @RequestMapping(value = "/login")
    public ResponseBean login(@RequestBody Map params) {
        String userName = (String) params.get("userName");
        String passWord = (String) params.get("passWord");
        User user = new User();
        user.setUserName(userName);
        String newPassWord = transferPassWord(passWord);
        user.setPassWord(newPassWord);
        ResponseBean bean = new ResponseBean();
        User userInfo = service.findByUser(user);
        if (userInfo == null) {
            throw new BizException(ErrorCodeConstant.BIZ_USER_ID_PASSWORD_INVALID);
        } else {
            session.setAttribute("user", userName);
            userInfo.setPassWord(null);
            bean.setData(userInfo);
            bean.setSuccess(true);
        }
        return bean;
    }

    /**
     * logout
     *
     * @return str
     */
    @RequestMapping(value = "/logout")
    public ResponseBean logout() {
        session.removeAttribute("user");
        ResponseBean bean = new ResponseBean();
        bean.setSuccess(true);
        return bean;
    }

    /**
     * 与前端加密获取token
     *
     * @return ResponseBean
     */
    @RequestMapping(value = "/token", method = RequestMethod.POST)
    public ResponseBean token() {
        String sessionId = session.getId().toString();
        String token = new SimpleHash(SecurityConstant.ALGORITHM, sessionId, SecurityConstant.SALT)
                .toString();
        ResponseBean responseBean = new ResponseBean();
        responseBean.setSuccess(true);
        responseBean.setData(token);
        return responseBean;
    }

    /**
     * 修改密码
     *
     * @param params 参数
     * @return ResponseBean
     */
    @RequestMapping(value = "/modifyPassWord", method = RequestMethod.POST)
    public ResponseBean modifyPassWord(@RequestBody Map<String, String> params) {
        User user = new User();
        user.setUserName(params.get("userName"));
        String oldPassWord = new SimpleHash(SecurityConstant.ALGORITHM,
                params.get("passWord"), SecurityConstant.SALT).toString();
        user.setPassWord(oldPassWord);

        User userInfo = service.findByUser(user);
        ResponseBean responseBean = new ResponseBean();
        if (userInfo == null) {
            throw new BizException(ErrorCodeConstant.BIZ_USER_ID_PASSWORD_INVALID);
        } else {
            String newPassWord = new SimpleHash(SecurityConstant.ALGORITHM,
                    params.get("newPassWord"), SecurityConstant.SALT).toString();
            user.setPassWord(newPassWord);
            if (service.updateUser(user)) {
                responseBean.setSuccess(true);
            } else {
                throw new BizException(ErrorCodeConstant.BIZ_USER_ID_DELETE_NOT_EXISTS);
            }

        }
        return responseBean;
    }

    /**
     * 密码解密并重新加密
     *
     * @param passWord passWord
     * @return String
     */
    private String transferPassWord(String passWord) {
        Des des = new Des();
        String sessionId = session.getId().toString();
        String key = new SimpleHash(SecurityConstant.ALGORITHM, sessionId, SecurityConstant.SALT).toString();
        String oldPassword = des.strDec(passWord, key, key, key);
        String newPassWord = new SimpleHash(SecurityConstant.ALGORITHM, oldPassword, SecurityConstant.SALT).toString();
        return newPassWord;
    }

}
